<?php
class Pages extends Controller {

	function __construct()
	{
		parent::Controller();
		$this->load->helper('url');
        $this->load->library(array('frontview', 'pagelib'));  
        $this->load->model('PageModel');

        $this->smartylib->assign('UserData', $this->session->userdata);
        if(!$this->session->userdata('logged_in')) redirect('/admin/login/');

	}

	function index()
	{
        $this->smartylib->assign('pages', $this->PageModel->getTree());
        $this->smartylib->assign('page', $this->frontview->view('admin/'));
		$this->smartylib->display('admin/layout.tpl');
	}

	function add($parent_id=1)
	{
		$this->smartylib->assign('page', $this->frontview->view($admin = 'admin/'));
		$this->smartylib->assign('pages', $this->PageModel->getTree());
		$this->smartylib->assign('parent_id', $parent_id);
		$this->smartylib->display('admin/layout.tpl');
	}

    function edit($id)
	{
        $doc = $this->PageModel->getPage($id);
        $doc->is_admin ? $admin = '/admin' : $admin = '';

        $this->smartylib->assign('page', $this->frontview->view('admin/'));
        $this->smartylib->assign('doc', $doc->toArray(true));
        $this->smartylib->assign('css', $this->pagelib->files('css'.$admin, 'css', $id));
        $this->smartylib->assign('js', $this->pagelib->files('js', 'js', $id));
		$this->smartylib->display('admin/layout.tpl');
	}

    function go()
	{
		if ($this->input->post('type',true))
			$this->PageModel->action($this->input->post('type',true), $this->input->post('id',true));
		else
			show_404('page');
	}
}